package com.chen.controller;


import com.chen.entity.MemberReceiveAddress;
import com.chen.service.MemberReceiveAddressService;
import com.chen.vo.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户收货地址表 前端控制器
 * </p>
 *
 * @author cws
 * @since 2024-11-25
 */
@RestController
@RequestMapping("/mall/memberReceiveAddress")
public class MemberReceiveAddressController {

    @Autowired
    private MemberReceiveAddressService addressService;

    @PostMapping("/add")
    @ApiOperation("新增收货信息")
    public Result addAddress(@RequestBody MemberReceiveAddress address){
        int i = addressService.create(address);
        if (i>0){
            return Result.success();
        }
        return Result.fail();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除收货信息")
    public Result deleteAddress(@PathVariable("id") Long id){
        int i = addressService.deleteAddress(id);
        if (i>0){
            return Result.success();
        }
        return Result.fail();
    }

    @PostMapping("/update/{id}")
    @ApiOperation("修改收货信息")
    public Result updateAddress(@PathVariable("id") Long id,@RequestBody MemberReceiveAddress address){
        int i = addressService.updateAddress(id,address);
        if (i>0){
            return Result.success();
        }
        return Result.fail();
    }

    @PostMapping("/updateDefault/{id}")
    @ApiOperation("修改默认收货信息")
    public Result updateDefaultAddr(@PathVariable("id") Long id,Integer status){
        int i = addressService.updateDefaultAddr(id,status);
        if (i>0){
            return Result.success();
        }
        return Result.fail();
    }

    @GetMapping("/list")
    @ApiOperation("获取当前用户所有收货信息")
    public Result addressList(){
        List<MemberReceiveAddress> umsMemberReceiveAddresses = addressService.addressList();
        return Result.success(umsMemberReceiveAddresses);
    }

    @GetMapping("/getAddr/{id}")
    @ApiOperation("根据收货地址id获取收货地址信息")
    public Result getAddressById(@PathVariable("id") Long id){
        MemberReceiveAddress addressById = addressService.getAddressById(id);
        return Result.success(addressById);
    }


}

